<?php
namespace Home\Controller;
use Common\Controller\CommonController;
class AwardController extends CommonController {
	
	function index(){
		$this->display('index');
	}

	/**
	 * [award_list 奖品]
	 * author  [lzm]
	 * time    2016年7月7日
 	 * @return [array] [description]
	 */
	//奖品列表
	public function award_list(){
		//$id=I('id');
		//print_r($id);exit;
		$award=M('award');
		$list=$award->order('id asc')->select();
		$this->assign('list',$list);
		$this->assign('id',$id);
		$this->display('index');
	
	}
	//添加奖品首页
	public function add_award_index(){
		$this->display('add_coupon');
	}
	

	//添加奖品
	public function add_award(){
		$type=I('type');
		$award=M('award');
		//接收参数
		$num=I('num');
		$cus_type=I('cus_type');
		$area=trim(I('area'));
		$tiaojian=trim(I('tiaojian'));
		$percent=trim(I('percent'));
		$xuzhi=html_entity_decode(I('xuzhi'));
		$huichang=trim(I('huichang'));
		$img=html_entity_decode(I('cover'));
		if (!is_numeric($num)) {
			error('数量必须是一个数字',$_SERVER['HTTP_REFERER']);
		}
		if (!is_numeric($percent) || $percent<0 || $percent>100) {
			error('概率不合法',$_SERVER['HTTP_REFERER']);
		}
		$name=trim(I('name'));
		$starttime=strtotime(trim(I('starttime')));
		$endtime=strtotime(trim(I('endtime')));	
		if ($starttime>=$endtime) {
				error('结束时间必须大于开始时间',$_SERVER['HTTP_REFERER']);
			}	
		$ctime=date('Y-m-d H:i:s',time());
		$mtime=date('Y-m-d H:i:s',time());
		$data=array(
			'cus_type'=>$cus_type,
			'type'=>$type,
			'name'=>$name,
			'num'=>$num,
			'percent'=>$percent,
			'area'=>$area,
			'tiaojian'=>$tiaojian,
			'xuzhi'=>$xuzhi,
			'huichang'=>$huichang,
			'img'=>$img,
			'starttime'=>$starttime,
			'endtime'=>$endtime,
			'ctime'=>$ctime,
			'mtime'=>$mtime,
			);
		//print_r($data);exit('$%$#@');
		$res_add=$award->data($data)->add();
		if (false !== $res_add) {
			 $this->redirect('Award/award_list',array('id' => $id));
		}else{
			  error('操作失败',$_SERVER['HTTP_REFERER']);
		}
		
	}
	
	//删除奖品
	public function del_award(){
		$id=I('id');
		$res=M('award')->where('id="'.$id.'"')->find();
		$cid=$res['cid'];
		$res_del=M('award')->where('id="'.$id.'"')->delete();
		if (false !== $res_del) {
		    $this->redirect('Award/award_list',array('id' => $cid));
		}else{
			error('操作失败',$_SERVER['HTTP_REFERER']);
		}
	}

	//修改奖品
	public function update_award_index(){
		$id=I('id');
		$list=M('award')->where('id="'.$id.'"')->find();
		$starttime=date('Y-m-d H:i:s',$list['starttime']);
		$endtime=date('Y-m-d H:i:s',$list['endtime']);
		$this->assign('starttime',$starttime);
		$this->assign('endtime',$endtime);
		$this->assign('list',$list);
		$this->display('update_coupon');

	}

	//修改奖品
	public function update_award(){
		$id=I('id');
		$type=I('type');
		//print_r($type);exit;
		$award=M('award');
		//接收参数
		$num=I('num');
		$cus_type=I('cus_type');
		$area=trim(I('area'));
		$tiaojian=trim(I('tiaojian'));
		$percent=trim(I('percent'));
		$xuzhi=html_entity_decode(I('xuzhi'));
		$huichang=trim(I('huichang'));
		$img=html_entity_decode(I('cover'));
		if (!is_numeric($num)) {
			error('数量必须是一个数字',$_SERVER['HTTP_REFERER']);
		}
		if (!is_numeric($percent) || $percent<0 || $percent>100) {
			error('概率不合法',$_SERVER['HTTP_REFERER']);
		}
		$name=trim(I('name'));
		$starttime=strtotime(trim(I('starttime')));
		$endtime=strtotime(trim(I('endtime')));	
		if ($starttime>=$endtime) {
				error('结束时间必须大于开始时间',$_SERVER['HTTP_REFERER']);
			}	
		$mtime=date('Y-m-d H:i:s',time());
		$data=array(
			'cus_type'=>$cus_type,
			'type'=>$type,
			'name'=>$name,
			'num'=>$num,
			'percent'=>$percent,
			'area'=>$area,
			'tiaojian'=>$tiaojian,
			'xuzhi'=>$xuzhi,
			'huichang'=>$huichang,
			'img'=>$img,
			'starttime'=>$starttime,
			'endtime'=>$endtime,
			'mtime'=>$mtime,
			);
		$res_save=$award->where('id="'.$id.'"')->data($data)->save();
		//echo M()->_sql();exit('@#$');
		if (false == $res_save){
			 error('操作失败',$_SERVER['HTTP_REFERER']);
		}else{
			$this->redirect('Award/award_list');
		}
	}


	//获奖名单列表
	public function winner_list(){
		$count = M('winner')->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=M('winner')->limit($Page->firstRow.','.$Page->listRows)->order('ctime desc')->select();
		$arr=array('page'=>$show,'list'=>$list);
		$this->assign('list',$arr);
		$this->display('winner_list');

	}

	
	//问卷调查结果
	public function answer(){
		$keyword=trim(I('keyword'));
		$where="(phone LIKE '%".$keyword."%')";
		$count = M('answer')->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,15);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list=M('answer')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('ctime desc')->select();
		$arr=array('page'=>$show,'list'=>$list);
		$this->assign('list',$arr);
		$this->display('answer');

	}

	//问卷调查答案列表
	public function answer_list(){
		$id=I('id');
		$list=M('answer')->where('id="'.$id.'"')->find();
		$answer=explode(',',$list['answer']);
		//print_r($answer);exit;
		$this->assign('list',$answer);
		$this->display();
	}
	
	//生成优惠券
	public function url(){
		//exit('@@@');
		$id=I('id');
		$url="http://sunwayuchoice.com/index.php/Gj/sendCoupon?id=".$id."";
		$this->ajaxReturn(array('s'=>'ok','m'=>$url));
		//$this->Ajaxreturn(array('s'=>"ok",'m'=>$url));
		//$this->Ajaxreturn(array('s'=>'ok','m'=>"success"));
	}
	

}